import axios, { AxiosInstance } from 'axios'
const baseUrl = '/api'

// 基础配置
const instance = axios.create({
    baseURL: baseUrl,
    timeout: 10_000,
})

// 请求拦截器
instance.interceptors.request.use((config) => {
    const token = localStorage.getItem('token')
    if (token) {
        config.headers['Authorization'] = token
    }
    return config
})

// 响应拦截器
instance.interceptors.response.use((response) => {
    if (response.data.code === 200) {
        return response.data
    } else {
        return Promise.reject(response.data)
    }
}, (error) => {
    return Promise.reject(error)
})

export default instance as AxiosInstance